<?php
$conf = new DOMDocument(); 
$conf->load( 'fields_conf.xml' ); 
$apps = $conf->getElementsByTagName( "app" );
$j = 0;
 
foreach( $apps as $app ) 
{

	$app_name = $app->getElementsByTagName( "name" )->item(0)->nodeValue; 
	if( $app_name != "correlator")
	{
		$app_names[$j] = $app_name; 
		$xml_field = $app->getElementsByTagName( "field" );
		$k = 0;
		unset($fields_names);
		foreach( $xml_field as $field ) 
		{
			$field_name = $field->getElementsByTagName( "name" )->item(0)->nodeValue;
	 		$fields_names[$k] = $field_name;
			$k++;
		}
		$fields[$j] = $fields_names;
		$j++;
	} 
}
 
$id = isset($_GET['id'])?$_GET['id']:-1;

if($id > -1){
	echo toJSON($fields[$id]);
}
else{
	echo toJSON($app_names);
}

function toJSON($array){
	$data=array(); $i=0;
	$total = count($array);
	foreach($array as $key=>$value){
		array_push($data,array(
			'value'=>$i++,
			'label'=>$value
		));
	}

	return json_encode(array(
		'total'=>$total,
		'data'=>$data
	));
}

?>
